package com.gvfish.backingbean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.gvfish.entity.Article;
import com.gvfish.entity.Link;
import com.gvfish.service.ArticleManager;
import com.gvfish.service.FetchManager;

@ManagedBean
@RequestScoped
public class ArticleBean {
	ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
	ArticleManager articleManager = (ArticleManager) ctx.getBean("articleManager");
	FetchManager fetchManager = (FetchManager)ctx.getBean("fetchManager");
	
	private List<Article> articles;
	private Long categoryId;
	private Article article=new Article();
	private DataModel<Article> articleModel = new ListDataModel<Article>();
	{
		articleModel.setWrappedData(articleManager.findAll());
	}
	public DataModel<Article> getModel(){
		return articleModel;
	}
	
	private String url;
	
	private List<Article> lastedArt;
	private List<Article> hotArts;

	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}

	public List<Article> getArticles() {
		if(articles==null){
			articles=articleManager.findAll();
		}
		return articles;
	}

	public ActionListener getDel() {
		return new ActionListener() {
			@Override
			public void processAction(ActionEvent arg0) throws AbortProcessingException {
				FacesContext ctx = FacesContext.getCurrentInstance();
				Long id = Long.parseLong((String) ctx.getExternalContext().getRequestParameterMap().get("id"));
				Long categoryId=Long.parseLong((String) ctx.getExternalContext().getRequestParameterMap().get("categoryId"));
				articleManager.del(categoryId,id);
				articles=articleManager.findAll();
			}
		};
	}
	//~~~~~~~~~~~~~~~action~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	public String save(){
		System.out.println(article);
		articleManager.save(categoryId,article);
		article = new Article();
		return "success";
	}
	public void del(ActionEvent event){
		Article article = articleModel.getRowData();
		articleManager.del(article.getKey());
		articleModel.setWrappedData(articleManager.findAll());
	}
	public String submit(){
		System.out.println("in submit");
		return "edit";
	}
	public void fetch(ActionEvent event){
		article =fetchManager.getCnFish(url);
		System.out.println(article);
	}

	public void setLastedArt(List<Article> lastedArt) {
		this.lastedArt = lastedArt;
	}

	public List<Article> getLastedArt() {
		if(lastedArt==null){
			lastedArt =articleManager.findLastedArt();
		}
		return lastedArt;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article getArticle() {
		return article;
	}

	public void setHotArts(List<Article> hotArts) {
		this.hotArts = hotArts;
	}

	public List<Article> getHotArts() {
		System.out.println("in get hotart");
		if(hotArts==null){
			hotArts = articleManager.findHotArt();
			System.out.println(hotArts);
		}
		return hotArts;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return url;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getCategoryId() {
		return categoryId;
	}
}
